// BranchPersist.cpp: implementation of the BranchPersist class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BranchPersist.h"
#include "Branch.h"
#include "DatabaseBase.h"
#include "DatabaseElement.h"
#include "BranchTable.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace PDMS::PDMSDBIF;
using namespace PDMS::PDMSDomain;
using namespace PDMS::PDMSFrame;

BEGIN_REGISTER_MAP( BranchPersist, PersistRegBase )
REGISTER( UE_RUNTIME_CLASS( Branch ), GET_REGISTER( PersistRegBase, VersionOne ) )
END_REGISTER_MAP();

BranchPersist::BranchPersist()
{

}

BranchPersist::~BranchPersist()
{

}

UESoft::ErrorStatus BranchPersist::Create( Persistable* pObj )
{
	UESoft::ErrorStatus es = CreatePaternity( pObj );
	if ( UESoft::eOk != es)
	{
		return es;
	}
	
	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Branch ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Branch* pBranch = static_cast<Branch*>( pObj );

	DatabaseBase* pbase = GetDatabase();
	DatabaseElement* pDatabaseElement;
	pDatabaseElement = pbase->GetDatabaseElement( _T("BranchData") );
	ASSERT( pDatabaseElement != NULL );

	BranchTable* pTable = static_cast<BranchTable*>( pDatabaseElement );
	es = pTable->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pTable->NewRecord();
	pTable->SetBuilFlag( pBranch->GetBuildValue() );
	pTable->SetCaseRef( pBranch->GetCasrefValue() );
	pTable->SetDescription( pBranch->GetDscpValue() );
	pTable->SetFluRef( pBranch->GetFlurefValue() );
	pTable->SetFunction( pBranch->GetFucValue() );
	pTable->SetHeadBore( pBranch->GetHboreValue() );
	pTable->SetHeadDir( pBranch->GetHdirValue() );
	pTable->SetHeadPos( pBranch->GetHposValue() );
	pTable->SetHeadRef( pBranch->GetHrefValue() );
	pTable->SetHstube( pBranch->GetHstubeValue() );
	pTable->SetID( pBranch->Id().ObjectID() );
	pTable->SetIspec( pBranch->GetIspecValue() );
	pTable->SetPaintRef( pBranch->GetPaintRef() );
	pTable->SetMatRef( pBranch->GetMatrefValue() );
	pTable->SetParentID( pBranch->OwnerId().ObjectID() );
	pTable->SetPressure( pBranch->GetPresValue() );
	pTable->SetPspec( pBranch->GetPspecValue() );
	pTable->SetPurpose( pBranch->GetPurpValue() );
	pTable->SetShopFlag( pBranch->GetShopValue() );
	pTable->SetTailBore( pBranch->GetTboreValue() );
	pTable->SetTailConn( pBranch->GetTconnValue() );
	pTable->SetTailDir( pBranch->GetTdirValue() );
	pTable->SetTailPos( pBranch->GetTposValue() );
	pTable->SetTailRef( pBranch->GetTrefValue() );
	pTable->SetTemperature( pBranch->GetTempValue() );
	pTable->SetTspec( pBranch->GetTspecValue() );
	pTable->SetHeadConn( pBranch->GetHconnValue() );

//	pTable->Update();

	return UESoft::eOk;
}

UESoft::ErrorStatus BranchPersist::Update( Persistable* pObj )
{
	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Branch ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Branch* pBranch = static_cast<Branch*>( pObj );

	DatabaseBase* pbase = GetDatabase();
	DatabaseElement* pDatabaseElement;
	pDatabaseElement = pbase->GetDatabaseElement( _T("BranchData") );
	ASSERT( pDatabaseElement != NULL );

	BranchTable* pTable = static_cast<BranchTable*>( pDatabaseElement );
	UESoft::ErrorStatus es = pTable->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pTable->UpdateRecord( pBranch->Id().ObjectID() );
	pTable->SetBuilFlag( pBranch->GetBuildValue() );
	pTable->SetCaseRef( pBranch->GetCasrefValue() );
	pTable->SetDescription( pBranch->GetDscpValue() );
	pTable->SetFluRef( pBranch->GetFlurefValue() );
	pTable->SetFunction( pBranch->GetFucValue() );
	pTable->SetHeadBore( pBranch->GetHboreValue() );
	pTable->SetHeadDir( pBranch->GetHdirValue() );
	pTable->SetHeadPos( pBranch->GetHposValue() );
	pTable->SetHeadRef( pBranch->GetHrefValue() );
	pTable->SetHstube( pBranch->GetHstubeValue() );
	pTable->SetID( pBranch->Id().ObjectID() );
	pTable->SetIspec( pBranch->GetIspecValue() );
	pTable->SetPaintRef( pBranch->GetPaintRef() );
	pTable->SetMatRef( pBranch->GetMatrefValue() );
	pTable->SetParentID( pBranch->OwnerId().ObjectID() );
	pTable->SetPressure( pBranch->GetPresValue() );
	pTable->SetPspec( pBranch->GetPspecValue() );
	pTable->SetPurpose( pBranch->GetPurpValue() );
	pTable->SetShopFlag( pBranch->GetShopValue() );
	pTable->SetTailBore( pBranch->GetTboreValue() );
	pTable->SetTailConn( pBranch->GetTconnValue() );
	pTable->SetTailDir( pBranch->GetTdirValue() );
	pTable->SetTailPos( pBranch->GetTposValue() );
	pTable->SetTailRef( pBranch->GetTrefValue() );
	pTable->SetTemperature( pBranch->GetTempValue() );
	pTable->SetTspec( pBranch->GetTspecValue() );
	pTable->SetHeadConn( pBranch->GetHconnValue() );

//	pTable->Update();

	return UESoft::eOk;
}

UESoft::ErrorStatus BranchPersist::Retrieve( Persistable*& pObj )
{
	UESoft::ErrorStatus es = ReadPaternity( pObj );
	if ( UESoft::eOk != es )
	{
		return es;
	}
	if ( NULL == pObj)
	{
		return UESoft::eNullObjectPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Branch ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Branch* pBranch = static_cast<Branch*>( pObj );

	DatabaseBase* pbase = GetDatabase();
	DatabaseElement* pDatabaseElement;
	pDatabaseElement = pbase->GetDatabaseElement( _T("BranchData") );
	ASSERT( pDatabaseElement != NULL );

	StdString strCondition;
	strCondition.Format( _T("ID = %ld"), pObj->Id().ObjectID() );

	BranchTable* pTable = static_cast<BranchTable*>( pDatabaseElement );
	es = pTable->Open( strCondition );
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pBranch->SetBuildValue( pTable->GetBuildFlag() );
	pBranch->SetCasrefValue( pTable->GetCaseRef() );
	pBranch->SetDscpValue( pTable->GetDescription() );
	pBranch->SetFlurefValue( pTable->GetFluRef() );
	pBranch->SetFucValue( pTable->GetFunction() );
	pBranch->SetHboreValue( pTable->GetHeadBore() );
	pBranch->SetHdirValue( pTable->GetHeadDir() );
	pBranch->SetHposValue( pTable->GetHeadPos() );
	pBranch->SetHrefValue( pTable->GetHeadRef() );
	pBranch->SetHstubeValue( pTable->GetHstube() );
	pBranch->SetIspecValue( pTable->GetIspec() );
	pBranch->SetPaintRef( pTable->GetPaintRef() );
	pBranch->SetMatrefValue( pTable->GetMatRef() );
	pBranch->OwnerId( ObjectId( pBranch->Id().EnviromentID(), pTable->GetParentID() ) );
	pBranch->SetPresValue( pTable->GetPressure() );
	pBranch->SetPspecValue( pTable->GetPspec() );
	pBranch->SetPurpValue( pTable->GetPurpose() );
	pBranch->SetShopValue( pTable->GetShopFlag() );
	pBranch->SetTboreValue( pTable->GetTailBore() );
	pBranch->SetTconnValue( pTable->GetTailConn() );
	pBranch->SetTdirValue( pTable->GetTailDir() );
	pBranch->SetTposValue( pTable->GetTailPos() );
	pBranch->SetTrefValue( pTable->GetTailRef() );
	pBranch->SetTempValue( pTable->GetTemperature() );
	pBranch->SetTspecValue( pTable->GetTspec() );
	pBranch->SetHconnValue( pTable->GetHeadConn() );

	
	return UESoft::eOk;
}


//DEL void BranchPersist::Update()
//DEL {
//DEL 	DatabaseBase* pbase = GetDatabase();
//DEL 	DatabaseElement* pDatabaseElement;
//DEL 	pDatabaseElement = pbase->GetDatabaseElement( _T("BranchData") );
//DEL 	ASSERT( pDatabaseElement != NULL );
//DEL 	pDatabaseElement->Update();
//DEL }
